package rs.etf.es.visualization;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import rs.etf.es.algorithm.Constants;
import rs.etf.es.algorithm.Node;
import rs.etf.es.algorithm.Util;

public class ChangeValues extends JDialog {
    private JTextField txtDisplay;
    private JButton btnDisplay;
    private JList<String> lstMyValues;
    private JList<String> lstParentValues;
    private DefaultListModel<String> myValuesModel = new DefaultListModel<String>();
    private DefaultListModel<String> parentValuesModel = new DefaultListModel<String>();
    private DefaultListModel<String> valuesModel = new DefaultListModel<String>();
    private JTextField txtName;
    private JTextField txtX;
    private JTextField txtY;
    private JList<String> lstValues;
    private JTextField txtNewValue;
    private JButton btnAddValue;

    private ArrayList<String> newValues = new ArrayList<String>();

    /**
     * Create the dialog.
     */
    public ChangeValues() {
	setIconImage(Toolkit.getDefaultToolkit().getImage(ChangeValues.class.getResource("/images/cpt.png")));
	setTitle("Change Values Dialog");
	setModal(true);
	setBounds(100, 100, 596, 407);
	getContentPane().setLayout(null);

	JScrollPane scrollPane = new JScrollPane();
	scrollPane.setBounds(10, 30, 180, 192);
	getContentPane().add(scrollPane);

	lstMyValues = new JList<String>();
	scrollPane.setViewportView(lstMyValues);

	JScrollPane scrollPane_1 = new JScrollPane();
	scrollPane_1.setBounds(235, 31, 178, 190);
	getContentPane().add(scrollPane_1);

	lstParentValues = new JList<String>();
	scrollPane_1.setViewportView(lstParentValues);

	JLabel lblMyValues = new JLabel("My values:");
	lblMyValues.setBounds(10, 11, 184, 14);
	getContentPane().add(lblMyValues);

	JLabel lblParentsValues = new JLabel("Parents values");
	lblParentsValues.setBounds(238, 11, 170, 14);
	getContentPane().add(lblParentsValues);

	txtDisplay = new JTextField();
	txtDisplay.setBounds(441, 167, 86, 20);
	getContentPane().add(txtDisplay);
	txtDisplay.setColumns(10);

	btnDisplay = new JButton("Display");
	btnDisplay.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		displayButtonClicked();
	    }
	});
	btnDisplay.setBounds(438, 195, 89, 23);
	getContentPane().add(btnDisplay);

	lstMyValues.setModel(myValuesModel);
	lstParentValues.setModel(parentValuesModel);

	JButton btnSave = new JButton("Save");
	btnSave.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		saveButtonClicked();
	    }
	});
	btnSave.setIcon(new ImageIcon(ChangeValues.class.getResource("/images/document-save.png")));
	btnSave.setBounds(441, 335, 86, 23);
	getContentPane().add(btnSave);

	txtName = new JTextField();
	txtName.setBounds(88, 233, 102, 20);
	getContentPane().add(txtName);
	txtName.setColumns(10);

	txtX = new JTextField();
	txtX.setBounds(88, 264, 102, 20);
	getContentPane().add(txtX);
	txtX.setColumns(10);

	txtY = new JTextField();
	txtY.setBounds(88, 295, 102, 20);
	getContentPane().add(txtY);
	txtY.setColumns(10);

	JLabel lblName = new JLabel("Name:");
	lblName.setBounds(20, 236, 46, 14);
	getContentPane().add(lblName);

	JLabel lblXCoordinate = new JLabel("X:");
	lblXCoordinate.setBounds(20, 267, 46, 14);
	getContentPane().add(lblXCoordinate);

	JLabel lblY = new JLabel("Y:");
	lblY.setBounds(20, 298, 46, 14);
	getContentPane().add(lblY);

	JScrollPane scrollPane_2 = new JScrollPane();
	scrollPane_2.setBounds(235, 230, 178, 87);
	getContentPane().add(scrollPane_2);

	lstValues = new JList<String>();
	scrollPane_2.setViewportView(lstValues);
	lstValues.setModel(valuesModel);

	txtNewValue = new JTextField();
	txtNewValue.setBounds(441, 267, 86, 20);
	getContentPane().add(txtNewValue);
	txtNewValue.setColumns(10);

	btnAddValue = new JButton("Add Value");
	btnAddValue.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
		addNewValue();
	    }
	});
	btnAddValue.setBounds(438, 294, 89, 23);
	getContentPane().add(btnAddValue);

    }

    public void setVisible(boolean display) {
	if (display) {
	    if (Node.selectedNode != null) {
		manualInit();
	    } else {
		return;
	    }

	} else {
	    valuesModel.clear();
	    parentValuesModel.clear();
	    myValuesModel.clear();
	}
	super.setVisible(display);
    }

    private void manualInit() {
	for (String value : Node.selectedNode.getValues()) {
	    myValuesModel.addElement(value);
	}

	if (Node.selectedNode.getParents().size() > 0) {
	    for (String parentValue : Node.selectedNode.getParents().get(0).getValues()) {
		parentValuesModel.addElement(parentValue);
	    }
	}

	for (String val : Node.selectedNode.getValues()) {
	    valuesModel.addElement(val);
	}

	txtName.setText(Node.selectedNode.getName());
	Integer x = Node.selectedNode.x;
	txtX.setText(x.toString());
	Integer y = Node.selectedNode.y;
	txtY.setText(y.toString());

    }

    protected void saveButtonClicked() {
	String selectedParentValue = lstParentValues.getSelectedValue();
	String selectedMyValue = lstMyValues.getSelectedValue();
	String parentName = null;
	if (Node.selectedNode.getParents().size() > 0) {
	    parentName = Node.selectedNode.getParents().get(0).getName();
	} else {
	    parentName = Node.NO_PARENT;
	}

	if (selectedParentValue == null) {
	    selectedParentValue = Node.NO_CONDITION;
	}

	Double newProbability;
	try {
	    newProbability = Double.parseDouble(txtDisplay.getText());
	} catch (NumberFormatException nfe) {
	    newProbability = Constants.INVALID;
	}

	if (newProbability != Constants.INVALID) {
	    Node.selectedNode.getMyCPT().setProbability(parentName, selectedMyValue, selectedParentValue, newProbability);
	}

	for (String str : newValues) {
	    Node.selectedNode.getValues().add(str);
	}

	newValues.clear();

	Node.selectedNode.setName(txtName.getText());
	try {
	    Node.selectedNode.x = Integer.parseInt(txtX.getText());
	    Node.selectedNode.y = Integer.parseInt(txtY.getText());
	} catch (NumberFormatException nfe) {
	    return;
	}
    }

    protected void displayButtonClicked() {

	String selectedParentValue = lstParentValues.getSelectedValue();
	String selectedMyValue = lstMyValues.getSelectedValue();
	String parentName = null;
	if (Node.selectedNode.getParents().size() > 0) {
	    parentName = Node.selectedNode.getParents().get(0).getName();
	} else {
	    parentName = Node.NO_PARENT;
	}

	if (selectedParentValue == null) {
	    selectedParentValue = Node.NO_CONDITION;
	}

	Double currentProbability = Node.selectedNode.getMyCPT().getProbability(parentName, selectedMyValue, selectedParentValue);
	txtDisplay.setText(Util.getDoubleAsString(currentProbability));

    }

    private void addNewValue() {
	String newVal = txtNewValue.getText();
	valuesModel.addElement(newVal);
	newValues.add(newVal);
    }
}
